package com.example.malltest0.common.utils;

import com.example.malltest0.Vo.HotVo;
import com.example.malltest0.mbg.model.Blogs;
import com.example.malltest0.mbg.model.Platform;
import com.example.malltest0.service.PlatformService;
import io.swagger.models.auth.In;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


@Component
public class RequestUtil {


    @Autowired
    private  PlatformService platformService;


    public static String httpRequest(String requestUrl) {
        //buffer用于接受返回的字符
        StringBuffer buffer = new StringBuffer();
        try {
            //建立URL，把请求地址给补全，其中urlencode（）方法用于把params里的参数给取出来
            URL url = new URL(requestUrl);
            //打开http连接
            HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
            httpUrlConn.setDoInput(true);
            httpUrlConn.setRequestProperty("content-type","text/json");
            httpUrlConn.connect();


            //获得输入
            InputStream inputStream = httpUrlConn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            //将bufferReader的值给放到buffer里
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            //关闭bufferReader和输入流
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            //断开连接
            httpUrlConn.disconnect();

        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回字符串
        return buffer.toString();
    }

    public static List<String> parseJSONWithJSONObject(String JsonData, String parseHot) throws JSONException {
        List<String> list=new ArrayList<>();
        JSONObject json = new JSONObject(JsonData);
        // JSONObject jsonObj = json.getJSONObject("trans_result");//获取json数组中的data项
        JSONObject data=null;
        String[] split = parseHot.split(":");
        for (int i = 0; i < split.length-2; i++) {
            data=json.getJSONObject(split[i]);
            json=data;
        }
        JSONArray paths =
                data.getJSONArray(split[split.length-2]);
        for (int i = 0; i < paths.length(); i++) {

            list.add(paths.getJSONObject(i).getString(split[split.length-1]));
        }
        return list;
    }
    public static Integer parseJSONWithBlogCount(String JsonData, String parseHot) throws JSONException {

        JSONObject json = new JSONObject(JsonData);
        // JSONObject jsonObj = json.getJSONObject("trans_result");//获取json数组中的data项
        JSONObject data=null;
        String[] split = parseHot.split(":");
        for (int i = 0; i < split.length-1; i++) {
            data=json.getJSONObject(split[i]);
            json=data;
        }

       return Integer.parseInt(data.get(split[split.length-1])+"");
    }
    public static Integer parseJSONWithBlog(String JsonData, String parseHot) throws JSONException {
//data:list:mblogid,text_raw,pic_ids,pic_infos:(pic_ids):bmiddle:url
        JSONObject json = new JSONObject(JsonData);
        // JSONObject jsonObj = json.getJSONObject("trans_result");//获取json数组中的data项
        JSONObject data=json.getJSONObject("data");
        JSONArray list = data.getJSONArray("list");
        for (int i = 0; i < list.length(); i++) {
            Blogs blogs=new Blogs();
            blogs.setId(list.getJSONArray(i).getString());
        }
        String[] split = parseHot.split(":");
        for (int i = 0; i < split.length-1; i++) {
            data=json.getJSONObject(split[i]);
            json=data;
        }

        return Integer.parseInt(data.get(split[split.length-1])+"");
    }
    public   List<List<String>>  getHot()  {
        List<Platform> platforms = platformService.selectAllPlatform();
        System.out.println(platforms);
        List<List<String>>list=new ArrayList<>();
        for (Platform platform : platforms) {
            String s = httpRequest(platform.getHotlink());
            List<String> hotVos = null;
            try {
                hotVos = parseJSONWithJSONObject(s,platform.getParsehot());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            list.add(hotVos);
        }
        return list;
    }


}
